package com.ys.controller.api.business;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.entity.business.TpAplusFile;
import com.ys.entity.business.TpBoutiqueSiteDetail;
import com.ys.service.business.TpAplusFileService;
import com.ys.service.business.TpBoutiqueSiteDetailService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 精品库站点详情信息
 */
@Controller
@RequestMapping("/api/inter/tpBoutiqueSiteDetail")
public class ApiInterTpBoutiqueSiteDetailController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTpBoutiqueSiteDetailController.class);
    private static Long MENU_ID = 10176L;

    @Autowired
    private CommonApiService<TpBoutiqueSiteDetail> commonApiService;
    @Autowired
    private TpBoutiqueSiteDetailService tpBoutiqueSiteDetailService;

    @Autowired
    private TpAplusFileService tpAplusFileService;

    /**
     * 保存
     */
    @RequestMapping("/toSave")
    @ResponseBody
    public R toSave(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            logger.info("精品库站点详情信息保存传参："+params);
            //参数验证
            String errorMsg = tpBoutiqueSiteDetailService.checkParams(params,MENU_ID);
            if(errorMsg!=null && !"".equals(errorMsg)){
                return R.error(errorMsg);
            }
            //A+信息验证
            /*
            Long id = Long.parseLong(params.get("id").toString());
            List<TpAplusFile> hasAplus = new TpAplusFile().setBoutiqueSiteDetailId(id).queryList();
            if(hasAplus==null || hasAplus.size()<=0){
                return R.error("A+为空！");
            }
             */
            logger.info("修改传参："+params);
            //修改
            return tpBoutiqueSiteDetailService.update(params);
        } catch (Exception e) {
            logger.error("保存精品库站点详情信息出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 精品库站点详情表信息新增,只新增站点信息
     */
    @RequestMapping("/toAddOnlySite")
    @ResponseBody
    public R toAddOnlySite(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //新增站点信息
            return tpBoutiqueSiteDetailService.toAddOnlySite(params);
        } catch (Exception e) {
            logger.error("新增出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @ResponseBody
    public R del(HttpServletRequest request, String ids,Long boutiqueId){
        try {
            //至少保留一条数据
            List<TpBoutiqueSiteDetail> hasOnlyOne = new TpBoutiqueSiteDetail().setBoutiqueId(boutiqueId).queryList();
            if(hasOnlyOne!=null && hasOnlyOne.size()==1){
                return R.error("至少保留一条站点信息数据！");
            }
            JSONArray jsonArray = JSONArray.parseArray(ids);
            if(jsonArray.size()>0){
                for(int i=0;i<jsonArray.size();i++){
                    //删除A+信息,根据详情Id
                    tpBoutiqueSiteDetailService.del(Long.parseLong(jsonArray.get(i).toString()));
                }
            }
            //删除A+信息,根据详情Id
            //tpAplusFileService.delByBoutiqueSiteDetailId(id);
            return R.ok();
        } catch (Exception e) {
            logger.error("删除精品库站点详情信息出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 根据精品库id查询精品库站点详情信息表
     */
    @RequestMapping("/searchDetailById")
    @ResponseBody
    public LayuiTableVoJA searchDetailById(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TpBoutiqueSiteDetail cond
    ){
        try {

            cond.addField("B.Code AS code");
            cond.addLeftJoin("TP_Boutique B ON B.id=A.BoutiqueId");
            cond.setOrderby("A.CreateTime ASC");
            LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
            return tableVoJA;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }




    /**
     * A+申请调用精品库-弹出框信息查询
     */
    @RequestMapping("/tpBoutiqueSiteAjtk")
    @ResponseBody
    public LayuiTableVoJA tpBoutiqueSiteAjtk(Model model, HttpServletRequest request, HttpServletResponse response,
                                              @ModelAttribute TpBoutiqueSiteDetail cond
    ){
        try {
            cond.addLeftJoin("TP_Boutique t2 on t2.Id = A.BoutiqueId");
            //cond.addLeftJoin("TP_AmazonRegionItem t3 on t3.Id = A.amazonRegionItemId");
            //cond.addLeftJoin("TP_AmazonRegion t4 on t4.code= t3.RegionCode ");

            cond.addLeftJoin("TB_Site S ON S.Id = A.SiteId")
                    .addLeftJoin("TP_AmazonRegion B ON B.Code = S.RegionCode")
                   // .addField("B.Name regionName")
                    .addLeftJoin("TB_Platform B1 ON B1.Code = B.PlatformCode")
                    //.addField("B1.Name platformName")
                    .addLeftJoin("TB_Country C ON C.Id = S.CountryId");
                    //.addField("C.Name countryName");


            cond.addField("t2.code as code, t2.name as name,t2.codeMerge as codeMerge,B1.Name AS platform,B.Name regionName,C.Name countryName");
            cond.where("t2.BoutiqueIsEffect = 1");
            cond.setOrderby("t2.CreateTime ASC");
            LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
            return tableVoJA;
        }catch (Exception e){
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }



}